﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using HsTec.Mobile.Green.Util;

namespace HsTec.Mobile.Green.DAL
{
    /// <summary>
    /// 数据访问类SingleProject。
    /// </summary>
    public class SingleProjectDAL
    {
        public SingleProjectDAL()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sExistsWhere)
        {
            return SQLHelper.Exists("pm_SingleProject", sExistsWhere);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HsTec.Mobile.Green.Model.SingleProject model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into pm_SingleProject(");
            strSql.Append("spno,listno,volumeno,spname,fix,cooperation,design,person,designer,proofreader,reviewer,charger,head,branchreviewer,Standardization,Investment,projectcost,designfees,accessories,surveydate,tp_id)");
            strSql.Append(" values (");
            strSql.Append("@spno,@listno,@volumeno,@spname,@fix,@cooperation,@design,@person,@designer,@proofreader,@reviewer,@charger,@head,@branchreviewer,@Standardization,@Investment,@projectcost,@designfees,@accessories,@surveydate,@tp_id)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@spno", SqlDbType.NVarChar,50),
					new SqlParameter("@listno", SqlDbType.NVarChar,20),
					new SqlParameter("@volumeno", SqlDbType.NVarChar,50),
					new SqlParameter("@spname", SqlDbType.NVarChar,200),
					new SqlParameter("@fix", SqlDbType.NVarChar,50),
					new SqlParameter("@cooperation", SqlDbType.Char,1),
					new SqlParameter("@design", SqlDbType.Char,2),
					new SqlParameter("@person", SqlDbType.Int,4),
					new SqlParameter("@designer", SqlDbType.Int,4),
					new SqlParameter("@proofreader", SqlDbType.Int,4),
					new SqlParameter("@reviewer", SqlDbType.Int,4),
					new SqlParameter("@charger", SqlDbType.Int,4),
					new SqlParameter("@head", SqlDbType.Int,4),
					new SqlParameter("@branchreviewer", SqlDbType.Int,4),
					new SqlParameter("@Standardization", SqlDbType.Int,4),
					new SqlParameter("@Investment", SqlDbType.Decimal,9),
					new SqlParameter("@projectcost", SqlDbType.Decimal,9),
					new SqlParameter("@designfees", SqlDbType.Decimal,9),
					new SqlParameter("@accessories", SqlDbType.NVarChar,200),
					new SqlParameter("@surveydate", SqlDbType.DateTime),
					new SqlParameter("@tp_id", SqlDbType.Int,4)};
            parameters[0].Value = model.spno;
            parameters[1].Value = model.listno;
            parameters[2].Value = model.volumeno;
            parameters[3].Value = model.spname;
            parameters[4].Value = model.fix;
            parameters[5].Value = model.cooperation;
            parameters[6].Value = model.design;
            parameters[7].Value = model.person;
            parameters[8].Value = model.designer;
            parameters[9].Value = model.proofreader;
            parameters[10].Value = model.reviewer;
            parameters[11].Value = model.charger;
            parameters[12].Value = model.head;
            parameters[13].Value = model.branchreviewer;
            parameters[14].Value = model.Standardization;
            parameters[15].Value = model.Investment;
            parameters[16].Value = model.projectcost;
            parameters[17].Value = model.designfees;
            parameters[18].Value = model.accessories;
            parameters[19].Value = model.surveydate;
            parameters[20].Value = model.tp_id;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(HsTec.Mobile.Green.Model.SingleProject model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update pm_SingleProject set ");
            strSql.Append("spno=@spno,");
            strSql.Append("listno=@listno,");
            strSql.Append("volumeno=@volumeno,");
            strSql.Append("spname=@spname,");
            strSql.Append("fix=@fix,");
            strSql.Append("cooperation=@cooperation,");
            strSql.Append("design=@design,");
            strSql.Append("person=@person,");
            strSql.Append("designer=@designer,");
            strSql.Append("proofreader=@proofreader,");
            strSql.Append("reviewer=@reviewer,");
            strSql.Append("charger=@charger,");
            strSql.Append("head=@head,");
            strSql.Append("branchreviewer=@branchreviewer,");
            strSql.Append("Standardization=@Standardization,");
            strSql.Append("Investment=@Investment,");
            strSql.Append("projectcost=@projectcost,");
            strSql.Append("designfees=@designfees,");
            strSql.Append("accessories=@accessories,");
            strSql.Append("surveydate=@surveydate,");
            strSql.Append("tp_id=@tp_id");
            strSql.Append(" where sp_id=@sp_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@sp_id", SqlDbType.Int,4),
					new SqlParameter("@spno", SqlDbType.NVarChar,50),
					new SqlParameter("@listno", SqlDbType.NVarChar,20),
					new SqlParameter("@volumeno", SqlDbType.NVarChar,50),
					new SqlParameter("@spname", SqlDbType.NVarChar,200),
					new SqlParameter("@fix", SqlDbType.NVarChar,50),
					new SqlParameter("@cooperation", SqlDbType.Char,1),
					new SqlParameter("@design", SqlDbType.Char,2),
					new SqlParameter("@person", SqlDbType.Int,4),
					new SqlParameter("@designer", SqlDbType.Int,4),
					new SqlParameter("@proofreader", SqlDbType.Int,4),
					new SqlParameter("@reviewer", SqlDbType.Int,4),
					new SqlParameter("@charger", SqlDbType.Int,4),
					new SqlParameter("@head", SqlDbType.Int,4),
					new SqlParameter("@branchreviewer", SqlDbType.Int,4),
					new SqlParameter("@Standardization", SqlDbType.Int,4),
					new SqlParameter("@Investment", SqlDbType.Decimal,9),
					new SqlParameter("@projectcost", SqlDbType.Decimal,9),
					new SqlParameter("@designfees", SqlDbType.Decimal,9),
					new SqlParameter("@accessories", SqlDbType.NVarChar,200),
					new SqlParameter("@surveydate", SqlDbType.DateTime),
					new SqlParameter("@tp_id", SqlDbType.Int,4)};
            parameters[0].Value = model.sp_id;
            parameters[1].Value = model.spno;
            parameters[2].Value = model.listno;
            parameters[3].Value = model.volumeno;
            parameters[4].Value = model.spname;
            parameters[5].Value = model.fix;
            parameters[6].Value = model.cooperation;
            parameters[7].Value = model.design;
            parameters[8].Value = model.person;
            parameters[9].Value = model.designer;
            parameters[10].Value = model.proofreader;
            parameters[11].Value = model.reviewer;
            parameters[12].Value = model.charger;
            parameters[13].Value = model.head;
            parameters[14].Value = model.branchreviewer;
            parameters[15].Value = model.Standardization;
            parameters[16].Value = model.Investment;
            parameters[17].Value = model.projectcost;
            parameters[18].Value = model.designfees;
            parameters[19].Value = model.accessories;
            parameters[20].Value = model.surveydate;
            parameters[21].Value = model.tp_id;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string sp_ids)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from pm_SingleProject ");
            strSql.Append(" where sp_id in ( " + sp_ids + " )");
            return SQLHelper.ExecuteNonQuery(strSql.ToString());
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HsTec.Mobile.Green.Model.SingleProject GetModel(int sp_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 sp_id,spno,listno,volumeno,spname,fix,cooperation,design,person,designer,proofreader,reviewer,charger,head,branchreviewer,Standardization,Investment,projectcost,designfees,accessories,surveydate,tp_id from pm_SingleProject ");
            strSql.Append(" where sp_id=@sp_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@sp_id", SqlDbType.Int,4)};
            parameters[0].Value = sp_id;

            HsTec.Mobile.Green.Model.SingleProject model = new HsTec.Mobile.Green.Model.SingleProject();
            DataSet ds = SQLHelper.ExecuteDataset(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sp_id"].ToString() != "")
                {
                    model.sp_id = int.Parse(ds.Tables[0].Rows[0]["sp_id"].ToString());
                }
                model.spno = ds.Tables[0].Rows[0]["spno"].ToString();
                model.listno = ds.Tables[0].Rows[0]["listno"].ToString();
                model.volumeno = ds.Tables[0].Rows[0]["volumeno"].ToString();
                model.spname = ds.Tables[0].Rows[0]["spname"].ToString();
                model.fix = ds.Tables[0].Rows[0]["fix"].ToString();
                model.cooperation = ds.Tables[0].Rows[0]["cooperation"].ToString();
                model.design = ds.Tables[0].Rows[0]["design"].ToString();
                if (ds.Tables[0].Rows[0]["person"].ToString() != "")
                {
                    model.person = int.Parse(ds.Tables[0].Rows[0]["person"].ToString());
                }
                if (ds.Tables[0].Rows[0]["designer"].ToString() != "")
                {
                    model.designer = int.Parse(ds.Tables[0].Rows[0]["designer"].ToString());
                }
                if (ds.Tables[0].Rows[0]["proofreader"].ToString() != "")
                {
                    model.proofreader = int.Parse(ds.Tables[0].Rows[0]["proofreader"].ToString());
                }
                if (ds.Tables[0].Rows[0]["reviewer"].ToString() != "")
                {
                    model.reviewer = int.Parse(ds.Tables[0].Rows[0]["reviewer"].ToString());
                }
                if (ds.Tables[0].Rows[0]["charger"].ToString() != "")
                {
                    model.charger = int.Parse(ds.Tables[0].Rows[0]["charger"].ToString());
                }
                if (ds.Tables[0].Rows[0]["head"].ToString() != "")
                {
                    model.head = int.Parse(ds.Tables[0].Rows[0]["head"].ToString());
                }
                if (ds.Tables[0].Rows[0]["branchreviewer"].ToString() != "")
                {
                    model.branchreviewer = int.Parse(ds.Tables[0].Rows[0]["branchreviewer"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Standardization"].ToString() != "")
                {
                    model.Standardization = int.Parse(ds.Tables[0].Rows[0]["Standardization"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Investment"].ToString() != "")
                {
                    model.Investment = decimal.Parse(ds.Tables[0].Rows[0]["Investment"].ToString());
                }
                if (ds.Tables[0].Rows[0]["projectcost"].ToString() != "")
                {
                    model.projectcost = decimal.Parse(ds.Tables[0].Rows[0]["projectcost"].ToString());
                }
                if (ds.Tables[0].Rows[0]["designfees"].ToString() != "")
                {
                    model.designfees = decimal.Parse(ds.Tables[0].Rows[0]["designfees"].ToString());
                }
                model.accessories = ds.Tables[0].Rows[0]["accessories"].ToString();
                if (ds.Tables[0].Rows[0]["surveydate"].ToString() != "")
                {
                    model.surveydate = DateTime.Parse(ds.Tables[0].Rows[0]["surveydate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["tp_id"].ToString() != "")
                {
                    model.tp_id = int.Parse(ds.Tables[0].Rows[0]["tp_id"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        public HsTec.Mobile.Green.Model.SingleProjectEntity GetModelEntity(int sp_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 sp_id,spno,listno,volumeno,spname,fix,cooperation,design,person,designer,proofreader,reviewer,charger,head,branchreviewer,Standardization,Investment,projectcost,designfees,accessories,surveydate,tp_id,personname,designername,proofreadername,reviewername,chargername,headname,branchreviewername,Standardizationname from v_pm_SingleProject ");
            strSql.Append(" where sp_id=@sp_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@sp_id", SqlDbType.Int,4)};
            parameters[0].Value = sp_id;

            HsTec.Mobile.Green.Model.SingleProjectEntity model = new HsTec.Mobile.Green.Model.SingleProjectEntity();
            DataSet ds = SQLHelper.ExecuteDataset(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sp_id"].ToString() != "")
                {
                    model.sp_id = int.Parse(ds.Tables[0].Rows[0]["sp_id"].ToString());
                }
                model.spno = ds.Tables[0].Rows[0]["spno"].ToString();
                model.listno = ds.Tables[0].Rows[0]["listno"].ToString();
                model.volumeno = ds.Tables[0].Rows[0]["volumeno"].ToString();
                model.spname = ds.Tables[0].Rows[0]["spname"].ToString();
                model.fix = ds.Tables[0].Rows[0]["fix"].ToString();
                model.cooperation = ds.Tables[0].Rows[0]["cooperation"].ToString();
                model.design = ds.Tables[0].Rows[0]["design"].ToString();
                if (ds.Tables[0].Rows[0]["person"].ToString() != "")
                {
                    model.person = int.Parse(ds.Tables[0].Rows[0]["person"].ToString());
                }
                if (ds.Tables[0].Rows[0]["designer"].ToString() != "")
                {
                    model.designer = int.Parse(ds.Tables[0].Rows[0]["designer"].ToString());
                }
                if (ds.Tables[0].Rows[0]["proofreader"].ToString() != "")
                {
                    model.proofreader = int.Parse(ds.Tables[0].Rows[0]["proofreader"].ToString());
                }
                if (ds.Tables[0].Rows[0]["reviewer"].ToString() != "")
                {
                    model.reviewer = int.Parse(ds.Tables[0].Rows[0]["reviewer"].ToString());
                }
                if (ds.Tables[0].Rows[0]["charger"].ToString() != "")
                {
                    model.charger = int.Parse(ds.Tables[0].Rows[0]["charger"].ToString());
                }
                if (ds.Tables[0].Rows[0]["head"].ToString() != "")
                {
                    model.head = int.Parse(ds.Tables[0].Rows[0]["head"].ToString());
                }
                if (ds.Tables[0].Rows[0]["branchreviewer"].ToString() != "")
                {
                    model.branchreviewer = int.Parse(ds.Tables[0].Rows[0]["branchreviewer"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Standardization"].ToString() != "")
                {
                    model.Standardization = int.Parse(ds.Tables[0].Rows[0]["Standardization"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Investment"].ToString() != "")
                {
                    model.Investment = decimal.Parse(ds.Tables[0].Rows[0]["Investment"].ToString());
                }
                if (ds.Tables[0].Rows[0]["projectcost"].ToString() != "")
                {
                    model.projectcost = decimal.Parse(ds.Tables[0].Rows[0]["projectcost"].ToString());
                }
                if (ds.Tables[0].Rows[0]["designfees"].ToString() != "")
                {
                    model.designfees = decimal.Parse(ds.Tables[0].Rows[0]["designfees"].ToString());
                }
                model.accessories = ds.Tables[0].Rows[0]["accessories"].ToString();
                if (ds.Tables[0].Rows[0]["surveydate"].ToString() != "")
                {
                    model.surveydate = DateTime.Parse(ds.Tables[0].Rows[0]["surveydate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["tp_id"].ToString() != "")
                {
                    model.tp_id = int.Parse(ds.Tables[0].Rows[0]["tp_id"].ToString());
                }
                model.branchreviewername = ds.Tables[0].Rows[0]["branchreviewername"].ToString();
                model.chargername = ds.Tables[0].Rows[0]["chargername"].ToString();
                model.designername = ds.Tables[0].Rows[0]["designername"].ToString();
                model.headname = ds.Tables[0].Rows[0]["headname"].ToString();
                model.personname = ds.Tables[0].Rows[0]["personname"].ToString();
                model.proofreadername = ds.Tables[0].Rows[0]["proofreadername"].ToString();
                model.reviewername = ds.Tables[0].Rows[0]["reviewername"].ToString();
                model.Standardizationname = ds.Tables[0].Rows[0]["Standardizationname"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sp_id,spno,listno,volumeno,spname,fix,cooperation,design,person,designer,proofreader,reviewer,charger,head,branchreviewer,Standardization,Investment,projectcost,designfees,accessories,surveydate,tp_id ");
            strSql.Append(" FROM pm_SingleProject ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        public DataView GetListEntity(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sp_id,spno,listno,volumeno,spname,fix,cooperation,design,person,designer,proofreader,reviewer,charger,head,branchreviewer,Standardization,Investment,projectcost,designfees,accessories,surveydate,tp_id,personname,designername,proofreadername,reviewername,chargername,headname,branchreviewername,Standardizationname ");
            strSql.Append(" FROM v_pm_SingleProject ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataView GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" sp_id,spno,listno,volumeno,spname,fix,cooperation,design,person,designer,proofreader,reviewer,charger,head,branchreviewer,Standardization,Investment,projectcost,designfees,accessories,surveydate,tp_id ");
            strSql.Append(" FROM pm_SingleProject ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        #endregion  成员方法
    }
}

